using System;
using System.Collections.Generic;
using UnityEngine;
using Core;
using Random = UnityEngine.Random;

namespace LST_Test
{
    /// <summary>
    /// 测试堆数据结构
    /// </summary>
    public class TestAStarNodeHeap : MonoBehaviour
    {
        void Start()
        {
            List<float> arr = new List<float>();
            int le = Random.Range(10, 100);
            for (int i = 0; i < le; i++)
            {
                arr.Add(Random.Range(0, 100));
            }

            var heap = new AStarHeap((lhs, rhs) => lhs.Priority <= rhs.Priority);

            var node = new Node[le];
            for (var i = 0; i < node.Length; i++)
            {
                node[i] = new Node();
            }

            for (var i = 0; i < node.Length; i++)
            {
                var tmp = node[i];
                var rand = arr[i];
                log.d($"LST - push_{i} - rand={rand}");
                heap.Push(tmp, rand);
            }

            arr.Sort();
            string sortArr = String.Empty;
            string part = " - ";
            for (int i = 0; i < arr.Count; i++)
            {
                sortArr += arr[i] + part;
            }

            log.d($"lst - sort - {sortArr}");

            log.d($"LST - before pop - {heap.DebugPrintNode()}");
            heap.Pop();
            arr.RemoveAt(0);
            log.d($"LST - after pop - {heap.DebugPrintNode()}");

            var rand2 = Random.Range(0, 100);
            log.d($"LST - push_one - rand={rand2}");
            heap.Push(new Node(), rand2);
            arr.Add(rand2);
            arr.Sort();
            log.d($"LST - after push one - {heap.DebugPrintNode()}");

            for (int i = 0; i < node.Length; i++)
            {
                var peek = heap.Peek.Priority;
                log.d($"lst - {heap.DebugPrintNode()}");
                if (Math.Abs(peek - arr[0]) > 0.1f)
                {
                    log.err($"Peek={peek}, arr={arr[0]}");
                }

                heap.Pop();
                arr.RemoveAt(0);
            }
        }
    }
}
